package view.logOnView;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.UserDao;
import model.User;
import util.DbUtil;
import util.StringUtil;
import view.bookAdmiView.BookAdmiMainFrm;
import view.readerView.ReaderMainFrm;
import view.systemAdmiView.SystemAdmiMainFrm;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.Color;
import java.awt.Toolkit;

public class LogOnFrm1 extends JFrame {

	private JPanel contentPane;
	private JTextField userNameTxt;

	private JRadioButton systemAdmiJrb;
	private JRadioButton teacherJrb ;
	private JRadioButton bookAdmiJrb;
	private JRadioButton studentJrb ;
	
	
	
	private DbUtil dbUtil=new DbUtil();
	private UserDao userDao=new UserDao();
	private JPasswordField passWordTxt;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			
			public void run() {
				try {
					LogOnFrm1 frame = new LogOnFrm1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}

	
		
	
	/**
	 * Create the frame.
	 */
	public LogOnFrm1() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(LogOnFrm1.class.getResource("/images/logon/登录.png")));
		//setBackground(new Color(240, 248, 255));
		
		
		
		//改变系统默认字体
		Font font=new Font("Dialog",Font.PLAIN,12);
		java.util.Enumeration keys=UIManager.getDefaults().keys();
		while(keys.hasMoreElements()) {
			Object key=keys.nextElement();
			Object value=UIManager.get(key);
			if(value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		
		setResizable(false);
		setTitle("管理员登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1178, 677);
		contentPane = new JPanel();
		//contentPane.setForeground(new Color(240, 248, 255));
		//contentPane.setBackground(new Color(250, 235, 215));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("图书管理系统");
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 40));
		lblNewLabel.setIcon(new ImageIcon(LogOnFrm1.class.getResource("/images/logon/图书管理系统.png")));
		
		JLabel userName = new JLabel("用户名:");
		userName.setBackground(Color.PINK);
		userName.setFont(new Font("微软雅黑", Font.PLAIN, 30));
		
		JLabel passWord = new JLabel("密   码:");
		passWord.setFont(new Font("微软雅黑", Font.PLAIN, 30));
		
		userNameTxt = new JTextField();
		userNameTxt.setFont(new Font("华文楷体", Font.BOLD, 30));
		userNameTxt.setColumns(10);
		
		passWordTxt = new JPasswordField();
		passWordTxt.setEchoChar('.');
		passWordTxt.setForeground(new Color(0, 0, 0));
		passWordTxt.setFont(new Font("华文行楷", Font.BOLD, 30));
		
		JButton logOnButton = new JButton("登录");		
		logOnButton.setIcon(new ImageIcon(LogOnFrm1.class.getResource("/images/logon/登录 (2).png")));
		logOnButton.setFont(new Font("楷体", Font.BOLD, 30));
		logOnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginActionPerformed(e);
			}
		});
		
		JButton resetButton = new JButton("重置");
		resetButton.setIcon(new ImageIcon(LogOnFrm1.class.getResource("/images/logon/重置 (1).png")));
		resetButton.setFont(new Font("楷体", Font.BOLD, 30));
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			resetValueActionPerformed(e);
			}
		});
		
		systemAdmiJrb = new JRadioButton("系统管理员");
		systemAdmiJrb.setBackground(new Color(230, 230, 250));
		systemAdmiJrb.setFont(new Font("微软雅黑", Font.PLAIN, 28));
		buttonGroup.add(systemAdmiJrb);
		
		teacherJrb = new JRadioButton("借阅者（老师）");
		teacherJrb.setBackground(new Color(255, 228, 225));
		teacherJrb.setFont(new Font("微软雅黑", Font.PLAIN, 28));
		buttonGroup.add(teacherJrb);
		teacherJrb.setSelected(true);
		
		bookAdmiJrb = new JRadioButton("图书管理员");
		bookAdmiJrb.setBackground(new Color(224, 255, 255));
		bookAdmiJrb.setFont(new Font("微软雅黑", Font.PLAIN, 28));
		buttonGroup.add(bookAdmiJrb);
		
		studentJrb = new JRadioButton("借阅者（学生）");
		studentJrb.setBackground(new Color(245, 245, 220));
		buttonGroup.add(studentJrb);
		studentJrb.setFont(new Font("微软雅黑", Font.PLAIN, 28));
		
		//passwordTxt = new JPasswordField();
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(359, Short.MAX_VALUE)
					.addComponent(logOnButton)
					.addGap(200)
					.addComponent(resetButton)
					.addGap(409))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(251)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(userName)
									.addGap(48))
								.addComponent(passWord, GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)))
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(systemAdmiJrb)
							.addGap(44)))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(passWordTxt, GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
						.addComponent(userNameTxt, GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(2)
							.addComponent(teacherJrb)
							.addGap(18)
							.addComponent(studentJrb)))
					.addGap(34)
					.addComponent(bookAdmiJrb)
					.addGap(93))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(405)
					.addComponent(lblNewLabel)
					.addContainerGap(421, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(40)
					.addComponent(lblNewLabel)
					.addGap(45)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(userName)
						.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
					.addGap(43)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(passWord)
						.addComponent(passWordTxt, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(systemAdmiJrb)
						.addComponent(teacherJrb)
						.addComponent(studentJrb)
						.addComponent(bookAdmiJrb))
					.addGap(49)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(logOnButton)
						.addComponent(resetButton))
					.addGap(44))
		);
		
		
		ImageIcon backIcon=new ImageIcon(getClass().getResource("/images/logon/dl3.jpeg"));
		 final JLabel backLabel=new JLabel(backIcon); // 创建背景标签
		
		backLabel.setSize(backIcon.getIconWidth(), backIcon.getIconHeight());
		
		 backLabel.setLocation(0, 0);
		 // 设置标签定位
		 contentPane.add(backLabel); // 添加标签到窗体
		setResizable(true);//可以改变窗体大小
		
		contentPane.setLayout(gl_contentPane);
		
		//设置JFrame居中显示
		this.setLocationRelativeTo(null);
	}

	/**
	 * 登录事件处理
	 * @param e
	 */
	private void loginActionPerformed(ActionEvent evt) {
		String userName=this.userNameTxt.getText();
		String password=new String(this.passWordTxt.getPassword());
		String userType="";
		if(this.systemAdmiJrb.isSelected()) {
			userType="系统管理员";
		}
		else if(this.bookAdmiJrb.isSelected()) {
			userType="图书管理员";
		}
		else if(this.teacherJrb.isSelected()) {
			userType="老师";
			
		}
		else if(this.studentJrb.isSelected()) {
			userType="学生";
			
		}
		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}
		User user=new User(userName,password,userType);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			User currentUser=userDao.login(con, user);
			if(currentUser!=null) {
				//JOptionPane.showMessageDialog(null, "登录成功！");
				
				//以下根据不同类型用户的登录，会出现不同功能界面
				if(systemAdmiJrb.isSelected()) {
					dispose();
					new SystemAdmiMainFrm().setVisible(true);
				}
				else if(teacherJrb.isSelected()) {
					dispose();
					new ReaderMainFrm().setVisible(true);
				}
				else if(studentJrb.isSelected()) {
					dispose();
					new ReaderMainFrm().setVisible(true);
				}

				else if(bookAdmiJrb.isSelected()) {
					dispose();
					new BookAdmiMainFrm().setVisible(true);
				}
				
				
				
			}else {
				JOptionPane.showMessageDialog(null, "用户信息错误，请重新输入！");
				this.userNameTxt.setText("");
				this.passWordTxt.setText("");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 重置事件处理
	 * @param evt
	 */
	private void resetValueActionPerformed(ActionEvent evt) {
		this.userNameTxt.setText("");
		this.passWordTxt.setText("");
	}
	
	public String getUserName() {
		String  userName=this.userNameTxt.getText();
		return userName;
	}
	
	public String getPassWord() {
		String password=new String(this.passWordTxt.getPassword());
		return password;
	}
}
